//
//  HomelTableViewCellTopView.swift
//  JKWeiBo
//
//  Created by 王冲 on 2017/12/19.
//  Copyright © 2017年 希爱欧科技有限公司. All rights reserved.
//

import UIKit

class HomelTableViewCellTopView: UIView {

    var topViewModel: HomeViewModel?
    {
        didSet{
           
            //给用户的头像赋值
            if let url = topViewModel?.user?.userImage_url
            {
                iconView.sd_setImage(with: url as URL, placeholderImage: UIImage(named:"avatar_default_big"))
            }
            // 给用户的类型赋值
            verifiedImage.image = topViewModel?.user?.verified_Image
            
            namelabel.text = topViewModel?.user?.name
            namelabel.width = JKwideHighSize().JKtextStringSize(string: topViewModel!.user!.name! as NSString, size: CGSize(width:JKscreenW-12-50-10-12-14-10,height:20), fontSize: 14).width
            // 是不是会员
            memberImage.x = namelabel.frame.maxX+10
            memberImage.image = topViewModel?.user?.mbrankImage
            //timelabel.text = homeViewModel!.created_at!
            // 时间赋值
            timelabel.text = topViewModel?.created_at
            timelabel.width = JKwideHighSize().JKtextStringSize(string: timelabel.text! as NSString, size: CGSize(width:(JKscreenW-12-50-10-12)/2.0,height:20), fontSize: 14).width
            // 来源
            fromePlace.text = topViewModel?.source
            fromePlace.width = JKwideHighSize().JKtextStringSize(string: fromePlace.text! as NSString, size: CGSize(width:(JKscreenW-12-50-10-12-10)-timelabel.width,height:20), fontSize: 14).width
            fromePlace.x = timelabel.frame.maxX+10
            
        }
    }
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        // 工具条的布局
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        
        // 1.添加子控件
        // 添加头像
        addSubview(self.iconView)
        // 添加认证图标
        addSubview(self.verifiedImage)
        // 添加昵称
        addSubview(self.namelabel)
        // 添加是不是会员
        addSubview(self.memberImage)
        // 添加时间
        addSubview(self.timelabel)
        // 添加来源
        addSubview(self.fromePlace)
        // 2.布局子控件
    }
    
    // MARK: - 懒加载
    // 1.头像
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named:"avatar_default_big"))
        iv.frame = CGRect(x: 12, y: 12, width: 50, height: 50)
        iv.contentMode = UIViewContentMode.scaleAspectFill
        return iv
        
    }()
    // 2.昵称
    private lazy var namelabel: UILabel = {
        
        let nameLabel = UILabel.createlabel(color: UIColor.black, fontSize: 14)
        nameLabel.frame = CGRect(x: 12+50+10, y: 12, width: JKscreenW-12-50-10-12, height: 16)
        return nameLabel
        
    }()
    // 3. 头像下面身份的图标
    private lazy var verifiedImage: UIImageView = {
        
        let verifiedImage = UIImageView()
        verifiedImage.frame = CGRect(x: 12+50-5, y: 12+50-5, width: 14, height: 14)
        return verifiedImage
    }()
    // 4. 会员图标
    private lazy var memberImage: UIImageView = {
        
        let memberImage = UIImageView()
        memberImage.y = 12
        memberImage.width = 14
        memberImage.height = 14
        return memberImage
    }()
    
    // 4.时间
    lazy var timelabel: UILabel = {
        let label = UILabel.createlabel(color: UIColor.black, fontSize: 14)
        label.x = 12+50+10
        label.y = 12+50-16
        label.height = 16
        label.textAlignment = NSTextAlignment.left
        return label
    }()
    // 5.来源
    lazy var fromePlace: UILabel = {
        
        let label = UILabel.createlabel(color: UIColor.JKTextzhuBlueColor(), fontSize: 14)
        label.y = 12+50-16
        label.height = 16
        label.textAlignment = NSTextAlignment.left
        return label
    }()
  
    
   
}
